"""
set是无序且无索引的
set使用{}声明
set支持数学里的交集，并集，差集等
"""

def testSet():
    fruitSet = {"apple", "banana"}
    print(isinstance(fruitSet, set))

    # 使用set构造函数，但是需要是两个()
    nameSetViaCon = set(("Alice", "Alan"))
    print(nameSetViaCon)

def testFunction():
    fruitSet = {"apple", "banana", "cherry"}

    # in和 not in 的使用
    print("apple in fruitSet? is {}".format("apple" in fruitSet))
    print("grape not in fruitSet? is {}".format("grape" not in fruitSet))
    print("len of set is {}".format(len(fruitSet)))

    # add 添加元素
    fruitSet.add("grape")
    print("add grape, fruitSet is {}".format(fruitSet))

    # remove 删除元素
    fruitSet.remove("grape")
    print("remove grape, fruitSet is {}".format(fruitSet))

    # 声明如下set
    firstSet = set("arbabadef")
    secondSet = set("bdcferad")

    print("firstSet is {}".format(firstSet))
    print("secondSet is {}".format(secondSet))

    # 取差集
    print("firstSet - secondSet is {}".format(firstSet - secondSet))

    # 取并集
    print("firstSet | secondSet is {}".format(firstSet | secondSet))

    # 取交集
    print("firstSet & secondSet is {}".format(firstSet & secondSet))

    # 只在一个集合中出现但是不都出现
    print("firstSet ^ secondeSet is {}".format(firstSet ^ secondSet))

    # 类似于列表生成式，set生成式也是可以的
    word = {char for char in "abreadfda" if char not in "abc"}
    print("word is {}".format(word))

testSet()
testFunction()